//
//  SecondTableViewController.m
//  2760
//
//  Created by 强 on 15/12/7.
//  Copyright (c) 2015年 杰润. All rights reserved.
//

#import "SecondTableViewController.h"
#import "ThreeTableViewController.h"
@interface SecondTableViewController ()

@property (nonatomic,strong)NSArray *dataSource;

//搜索栏
@property (nonatomic,strong)UISearchBar * searchBar;
//搜索结果显示
@property (nonatomic,strong)UISearchDisplayController  *disPlay;

@property (nonatomic,strong)NSMutableArray *searchResult;
@end

@implementation SecondTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.titleName;
    self.dataSource = @[@"1",@"2",@"3"];
    self.searchResult = [[NSMutableArray alloc] init];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
    self.searchBar.placeholder = @"搜索";
    self.tableView.tableHeaderView = self.searchBar;
   //实现两个协议方法
    self.disPlay = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
    self.disPlay.searchResultsDataSource = self;
    self.disPlay.searchResultsDelegate = self;
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    if (tableView == self.tableView) {
        return self.dataSource.count;
    }else {
        [self.searchResult removeAllObjects];
        for (NSString *obj  in self.dataSource) {
            NSRange range = [obj rangeOfString:self.searchBar.text];
            if (range.location != NSNotFound) {
                [self.searchResult addObject:obj];
            }
        }
        return self.searchResult.count;
    }

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuseID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseID];
    }
    if (tableView == self.tableView) {
        cell.textLabel.text = self.dataSource[indexPath.row];
        
    }else {
        cell.textLabel.text = self.searchResult[indexPath.row];
    }
    
    // Configure the cell...
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ThreeTableViewController *three = [[ThreeTableViewController alloc] init];
    [self.navigationController pushViewController:three animated:YES];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
